package ar.com.gep.soccer.facade.impl;

import java.util.List;

import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import ar.com.cdt.framework.business.bo.crud.GenericCRUDBO;
import ar.com.cdt.framework.business.exception.CDTFrameworkBusinessException;
import ar.com.cdt.framework.core.exception.CDTFrameworkException;
import ar.com.cdt.framework.core.pager.Page;
import ar.com.cdt.framework.core.pager.ResultSet;
import ar.com.cdt.framework.tools.dozer.mappers.DozerMapper;
import ar.com.gep.soccer.entities.Team;
import ar.com.gep.soccer.exception.TeamBusinessException;
import ar.com.gep.soccer.facade.TeamFacade;
import ar.com.gep.soccer.services.dto.TeamDTO;

@Component
public class TeamFacadeImpl implements TeamFacade {

	private static final Logger log = Logger.getLogger(TeamFacadeImpl.class);

	@Autowired
	@Qualifier("team.bo")
	private GenericCRUDBO<Team> teamBO;

	@Autowired
	@Qualifier(value = "team.dozer")
	private DozerMapper<TeamDTO, Team> teamMapper;

	/*
	 * (non-Javadoc)
	 * 
	 * @see ar.com.gepp.cup.facade.TeamFacade#getAllTeams()
	 */
	public List<TeamDTO> getAllTeams() throws TeamBusinessException {
		log.infov("TeamFacade - {0}(): Recuperando todos los equipos.", "getAll");
		List<TeamDTO> teamDTOs;
		List<Team> teams;
		try {
			teams = teamBO.getAll();
			teamDTOs = teamMapper.mapToDTO(teams);
		} catch (CDTFrameworkException exception) {
			log.errorv(exception, "TeamFacade - {0}(): Error al recuperar todos los equipos.", "getAll");
			throw new TeamBusinessException("Error al recuperar todos los equipos", exception);
		}
		return teamDTOs;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ar.com.gepp.cup.facade.TeamFacade#getTeamById(java.lang.Long)
	 */
	public TeamDTO getTeamById(final Long id) throws TeamBusinessException {
		log.infov("TeamFacade - {0}(): Recuperando un equipo con id {1}.", "getTeamById", id);
		TeamDTO teamDTO;
		Team team;
		try {
			team = teamBO.getById(id);
			if (team == null) {
				log.errorv("TeamFacade - {0}(): Error al recuperar un equipo con id {1}.", "getTeamById", id);
				throw new TeamBusinessException("Error al recuperar un equipo con id " + id);
			}
			teamDTO = teamMapper.mapToDTO(team);
		} catch (CDTFrameworkException exception) {
			log.errorv(exception, "TeamFacade - {0}(): Error al recuperar un equipo con id {1}.", "getTeamById", id);
			throw new TeamBusinessException("Error al recuperar un equipo con id " + id, exception);
		}
		return teamDTO;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ar.com.gepp.cup.facade.TeamFacade#saveTeam(ar.com.gepp.cup.dto.TeamDTO)
	 */
	public void saveTeam(final TeamDTO teamDTO) throws TeamBusinessException {
		log.infov("TeamFacade - {0}(): Almacenando un equipo.", "saveTeam");
		Team team;
		try {
			team = teamMapper.mapToEntity(teamDTO);
			teamBO.create(team);
		} catch (CDTFrameworkException exception) {
			log.errorv(exception, "TeamFacade - {0}(): Error al almacenar un equipo..", "saveTeam");
			throw new TeamBusinessException("Error al almacenar un equipo.", exception);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ar.com.gepp.cup.facade.TeamFacade#update(ar.com.gepp.cup.dto.TeamDTO)
	 */
	public void updateTeam(final TeamDTO teamDTO) throws TeamBusinessException {
		log.infov("TeamFacade - {0}(): Actualizando un equipo.", "updateTeam");
		Team team;
		try {
			team = teamMapper.mapToEntity(teamDTO);
			teamBO.update(team);
		} catch (CDTFrameworkException exception) {
			log.errorv(exception, "TeamFacade - {0}(): Error al actualizar un equipo..", "updateTeam");
			throw new TeamBusinessException("Error al actualizar un equipo.", exception);
		}
	}

	@Override
	public ResultSet<TeamDTO> getAllTeams(final Page page) throws TeamBusinessException {
		log.infov("TeamFacade - {0}(): Obteniendo lista de teams", "getAllTeams");
		ResultSet<Team> result;
		ResultSet<TeamDTO> resultDTO = new ResultSet<TeamDTO>();
		try {
			result = teamBO.getAll(page);
			resultDTO.setResult(teamMapper.mapToDTO(result.getResult()));
			resultDTO.setTotalResults(result.getTotalResults());
			resultDTO.setPage(result.getPage());
		} catch (CDTFrameworkBusinessException exception) {
			log.errorv(exception, "ProjectFacade BO - {0}(): Error al obtener todas las categorias.", "getAllCategories");
			throw new TeamBusinessException(exception);
		}

		return resultDTO;		
	}
	
	

}
